package com.nandi.deviceinstall.ui.adapter

import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.LazyHeaders
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.nandi.deviceinstall.R
import com.nandi.deviceinstall.bean.BindRecordBean
import com.nandi.deviceinstall.common.Constant
import com.nandi.deviceinstall.util.SharedTool

class RecordsAdapter : BaseQuickAdapter<BindRecordBean.DataBean.RecordsBean, BaseViewHolder> {
    var context: Context? = null

    private val listDeviceType = arrayOf("地裂缝", "墙裂缝", "地面倾斜", "智能报警器", "雨量计", "泥位计")

    constructor(layoutResId: Int) : super(layoutResId)

    override fun convert(helper: BaseViewHolder, item: BindRecordBean.DataBean.RecordsBean?) {
        if (context == null){
            context = helper.getView<ImageView>(R.id.ivRecord).context
        }

        helper.setText(R.id.disasterName, item!!.disasterName)
                .setText(R.id.disasterNum, item!!.disNo)
                .setText(R.id.cityName, item!!.cityName)
                .setText(R.id.townName, item!!.townName)
                .setText(R.id.time,item!!.bindTime)
                .setText(R.id.deviceType,listDeviceType[item!!.devType])
                .setVisible(R.id.deviceType,true)
                .addOnClickListener(R.id.delete)

        if (item.devImage!!.isNotEmpty()){
            val split = item.devImage!!.split(",")
            if (split[0].isNotEmpty()) {
                var imagePath = "${Constant.fileUrl}${split[0]}&type=6"
                val headers = LazyHeaders.Builder().addHeader("Authorization", SharedTool.get(context,
                        Constant.TOKEN, "") as String).build()
                val requestOptions = RequestOptions().error(R.mipmap.default_image)
                        .placeholder(R.mipmap.default_image).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true)
                Glide.with(context!!)
                        //.asBitmap()

                        .load(GlideUrl(imagePath, headers))
                        .apply(requestOptions)
                        .into(helper.getView(R.id.ivRecord))
            }
        }
    }
}